﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibHuman;
namespace Human
{
	/// <summary>
	/// Summary description for frmDanhSachVaKetQuaNangLuongbyt.
	/// </summary>
	public class frmKQNangBacLuong : System.Windows.Forms.Form
	{
		private AccessData mDB=new AccessData();
		private DataSet ds =new DataSet();
		private DataSet dsxml =new DataSet();
		
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button butThoat;
		private System.Windows.Forms.Button butXem;
		private System.Windows.Forms.ComboBox cboThang;
		private System.Windows.Forms.ComboBox cboNam;
        private GroupBox groupBox1;
        private RadioButton radexcel;
        private RadioButton rdbMayIn;
        private RadioButton rdbManHinh;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmKQNangBacLuong()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
        string ngay = "", thang = "", nam = "";
		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmKQNangBacLuong));
            this.label1 = new System.Windows.Forms.Label();
            this.cboThang = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.cboNam = new System.Windows.Forms.ComboBox();
            this.butThoat = new System.Windows.Forms.Button();
            this.butXem = new System.Windows.Forms.Button();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.radexcel = new System.Windows.Forms.RadioButton();
            this.rdbMayIn = new System.Windows.Forms.RadioButton();
            this.rdbManHinh = new System.Windows.Forms.RadioButton();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(12, 19);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(53, 16);
            this.label1.TabIndex = 9;
            this.label1.Text = "Tháng :";
            // 
            // cboThang
            // 
            this.cboThang.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboThang.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192)))));
            this.cboThang.Items.AddRange(new object[] {
            "01",
            "02",
            "03",
            "04",
            "05",
            "06",
            "07",
            "08",
            "09",
            "10",
            "11",
            "12"});
            this.cboThang.Location = new System.Drawing.Point(67, 18);
            this.cboThang.Name = "cboThang";
            this.cboThang.Size = new System.Drawing.Size(48, 21);
            this.cboThang.TabIndex = 0;
            this.cboThang.Validated += new System.EventHandler(this.cboThang_Validated);
            this.cboThang.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cboThang_KeyDown);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.Location = new System.Drawing.Point(121, 19);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(43, 16);
            this.label2.TabIndex = 3;
            this.label2.Text = "Năm :";
            // 
            // cboNam
            // 
            this.cboNam.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboNam.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192)))));
            this.cboNam.Items.AddRange(new object[] {
            "1997",
            "1998",
            "1999",
            "2000",
            "2001",
            "2002",
            "2003",
            "2004",
            "2005",
            "2006",
            "2007",
            "2008",
            "2009",
            "2010",
            "2011",
            "2012",
            "2013",
            "2014",
            "2015",
            "2016",
            "2017",
            "2018",
            "2019",
            "2020",
            "2021",
            "2022",
            "2023",
            "2024",
            "2025",
            "2026",
            "2027",
            "2028",
            "2029",
            "2030",
            "2031",
            "2032",
            "2033",
            "2034",
            "2035",
            "2036",
            "2037",
            "2038",
            "2039",
            "2040"});
            this.cboNam.Location = new System.Drawing.Point(170, 18);
            this.cboNam.Name = "cboNam";
            this.cboNam.Size = new System.Drawing.Size(60, 21);
            this.cboNam.TabIndex = 1;
            this.cboNam.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cboNam_KeyDown);
            // 
            // butThoat
            // 
            this.butThoat.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butThoat.Image = ((System.Drawing.Image)(resources.GetObject("butThoat.Image")));
            this.butThoat.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butThoat.Location = new System.Drawing.Point(124, 111);
            this.butThoat.Name = "butThoat";
            this.butThoat.Size = new System.Drawing.Size(60, 28);
            this.butThoat.TabIndex = 7;
            this.butThoat.Text = "Thoát";
            this.butThoat.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butThoat.UseVisualStyleBackColor = true;
            this.butThoat.Click += new System.EventHandler(this.butThoat_Click);
            // 
            // butXem
            // 
            this.butXem.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butXem.Image = ((System.Drawing.Image)(resources.GetObject("butXem.Image")));
            this.butXem.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butXem.Location = new System.Drawing.Point(35, 111);
            this.butXem.Name = "butXem";
            this.butXem.Size = new System.Drawing.Size(75, 28);
            this.butXem.TabIndex = 5;
            this.butXem.Text = "  Báo cáo";
            this.butXem.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butXem.UseVisualStyleBackColor = true;
            this.butXem.Click += new System.EventHandler(this.butXem_Click);
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.radexcel);
            this.groupBox1.Controls.Add(this.rdbMayIn);
            this.groupBox1.Controls.Add(this.rdbManHinh);
            this.groupBox1.Location = new System.Drawing.Point(12, 52);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(220, 41);
            this.groupBox1.TabIndex = 10;
            this.groupBox1.TabStop = false;
            // 
            // radexcel
            // 
            this.radexcel.Location = new System.Drawing.Point(158, 16);
            this.radexcel.Name = "radexcel";
            this.radexcel.Size = new System.Drawing.Size(60, 15);
            this.radexcel.TabIndex = 3;
            this.radexcel.Text = "Excel";
            // 
            // rdbMayIn
            // 
            this.rdbMayIn.Location = new System.Drawing.Point(91, 16);
            this.rdbMayIn.Name = "rdbMayIn";
            this.rdbMayIn.Size = new System.Drawing.Size(72, 15);
            this.rdbMayIn.TabIndex = 2;
            this.rdbMayIn.Text = "Máy in";
            // 
            // rdbManHinh
            // 
            this.rdbManHinh.Checked = true;
            this.rdbManHinh.Location = new System.Drawing.Point(14, 16);
            this.rdbManHinh.Name = "rdbManHinh";
            this.rdbManHinh.Size = new System.Drawing.Size(72, 15);
            this.rdbManHinh.TabIndex = 1;
            this.rdbManHinh.TabStop = true;
            this.rdbManHinh.Text = "Màn hình";
            // 
            // frmKQNangBacLuong
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(245, 150);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.butThoat);
            this.Controls.Add(this.butXem);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.cboNam);
            this.Controls.Add(this.cboThang);
            this.ForeColor = System.Drawing.Color.DarkBlue;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmKQNangBacLuong";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Danh sách và kết quả nâng bậc lương (Theo bộ y tế)";
            this.Load += new System.EventHandler(this.frmDanhSachVaKetQuaNangLuong_Load);
            this.groupBox1.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void butThoat_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private string stringtoMMDDYYYY(string s)
		{
			return s.Substring(3,2)+"/"+s.Substring(0,2)+"/"+s.Substring(6,4);
		}

		private void print(bool prn)
		{		
			try
			{
                ngay = DateTime.Now.Day.ToString().PadLeft(2, '0');
                thang = cboThang.Text.Trim().PadLeft(2, '0');
                nam = cboNam.Text.Trim();
				dsxml.ReadXml("..\\xml\\BaoCaoN1.xml");
				dsxml.Clear();									
				string sql="SELECT '"+cboThang.SelectedItem.ToString()+"' as ct,";
				sql+=" '"+cboNam.SelectedItem.ToString()+"' as cn,bp.madv,bp.tenbp,";
				sql+=" trim(l.ho)+' '+trim(l.ten) as B,";
				sql+=" iif(l.phai='Nam',iif(l.ngaysinh<>'',year(l.ngaysinh),l.namsinh),'') as c1,";
				sql+=" iif(l.phai='Nữ',iif(l.ngaysinh<>'',year(l.ngaysinh),l.namsinh),'') as c2,";
				sql+=" mn.diengiai as c3,hd.mangach as c4,hd.hs_lc as c5,";
				sql+=" to_char(hd.ngayhd,'dd/mm/yyyy') as c6,hd.mangach as c8,";
				sql+=" hd.hs_lc as mucluong,hd.hs_lc as mucluongmoi,hd.mangach,";
				sql+=" 00 as c10,hd.luonghd,hd.bac,";
				sql+=" '28/'+'"+cboThang.SelectedItem.ToString()+"'+'/'+'"+cboNam.SelectedItem.ToString()+"' as c101,";			
				sql+=" int(((datevalue('28/'+'"+cboThang.SelectedItem.ToString()+"'+'/'+'"+cboNam.SelectedItem.ToString()+"'))";
				sql+=" -datevalue(hd.ngayhd))/30 - iif(left(trim(hd.loaingach),1)='A',3*12,2*12)) as c11,";
				sql+=" 00 as c12,";
				sql+=" to_char(dateadd('m', IIf(left(trim(hd.loaingach),1)='A',3*12,2*12),hd.ngayhd),'dd/mm/yyyy') as tdnangls";				
				sql+=" FROM llnv l,hopdong hd,mahd mh,mangach mn,bophan bp";
				sql+=" WHERE isnull(hd.ngaynghi) and l.manv=hd.manv and hd.loaihd<>'HD' and mh.mahd=hd.loaihd and bp.madv=l.madv and ";
				sql+=" hd.mangach=mn.ma and (hd.mangach='16.117' or hd.mangach='16.133')";
				//sql+=" and datevalue(hd.ngayhd) <=(datevalue('28/'+'"+cboThang.SelectedItem.ToString()+"'+'/'+'"+cboNam.SelectedItem.ToString()+"'))";			
				sql+=" and dateadd('m', IIf(left(trim(hd.loaingach),1)='A',";
				sql+=" hd.ngayhd) is not null  and dateadd('m', IIf(left(trim(hd.loaingach),1)='A',3*12,2*12),hd.ngayhd) <=(datevalue('28/'+'"+cboThang.SelectedItem.ToString()+"'+'/'+'"+cboNam.SelectedItem.ToString()+"'))";
				sql+=" order by bp.madv";
				ds=mDB.get_data(sql);	
			
				string sql1=" SELECT sum(iif(l.manv<>null,1,0)) as tongsocanbo";			
				sql1+=" FROM llnv l,hopdong hd,mahd mh,mangach mn";
				sql1+=" WHERE isnull(hd.ngaynghi) and l.manv=hd.manv and hd.loaihd<>'HD' and hd.loaihd=mh.mahd and hd.mangach=mn.ma";						
				sql1+=" and hd.ngayhd <=(datevalue('28/'+'"+cboThang.SelectedItem.ToString()+"'+'/'+'"+cboNam.SelectedItem.ToString()+"'))";
				DataSet ds1=new DataSet();
				ds1=mDB.get_data(sql1);	

				string sql2="SELECT Sum(IIf(l.manv Is Not Null,1,0)) AS tongsocanbo_bl";
				sql2+=" FROM llnv AS l, hopdong AS hd, mahd AS mh, mangach AS mn";
				sql2+=" WHERE isnull(hd.ngaynghi) and (((l.manv)=[hd].[manv]) AND ((hd.LOAIHD)<>'HD' And";
				sql2+=" (hd.LOAIHD)=[mh].[mahd]) AND ((hd.MANGACH)=[mn].[ma])) and";
				sql2+=" dateadd('m', IIf(left(trim(hd.loaingach),1)='A',3*12,2*12),";
				sql2+=" hd.ngayhd) <>''  and dateadd('m', IIf(left(trim(hd.loaingach),1)='A',3*12,2*12),hd.ngayhd) <=(datevalue('28/'+'"+cboThang.SelectedItem.ToString()+"'+'/'+'"+cboNam.SelectedItem.ToString()+"'))";						
				DataSet ds2=new DataSet();
				ds2=mDB.get_data(sql2);

				foreach(DataRow r in ds.Tables[0].Rows)
				{					
					if(r["B"].ToString()!="")
					{
						DataRow nr=dsxml.Tables[0].NewRow();
						nr["ct"]=(r["ct"].ToString()=="")?"":(r["ct"].ToString());
						nr["cn"]=(r["cn"].ToString()=="")?"":(r["cn"].ToString());
						nr["madv"]=(r["madv"].ToString()=="")?"":(r["madv"].ToString());
						nr["tenbp"]=(r["tenbp"].ToString()=="")?"":(r["tenbp"].ToString());
						nr["B"]=(r["B"].ToString()=="")?"":(r["B"].ToString());
						nr["bac"]=int.Parse(r["bac"].ToString());
						nr["c1"]=(r["c1"].ToString());
						nr["c2"]=(r["c2"].ToString());
						nr["c3"]=(r["c3"].ToString());
						nr["c4"]=(r["c4"].ToString());
						nr["c5"]=(r["c5"].ToString()=="")?0:decimal.Parse(r["c5"].ToString());
						nr["c6"]=(r["c6"].ToString());
						nr["c8"]=(r["c8"].ToString());				
						nr["mucluong"]=(r["mucluong"].ToString()=="")?0:decimal.Parse(r["mucluong"].ToString());				
						nr["mucluongmoi"]=mDB.next_hslc(r["mangach"].ToString(),decimal.Parse(r["mucluong"].ToString())).ToString();				
						nr["mangach"]=(r["mangach"].ToString());
						nr["c101"]=(r["c101"].ToString());
						nr["c10"]=(nr["mucluongmoi"].ToString()=="" && r["c5"].ToString()=="")?0:(decimal.Parse(nr["mucluongmoi"].ToString())-decimal.Parse(r["c5"].ToString()));
						nr["c11"]=(r["c11"].ToString()=="")?0:decimal.Parse(r["c11"].ToString());
						nr["c12"]=(r["c11"].ToString()=="" && (nr["mucluongmoi"].ToString()=="") && (nr["c5"].ToString()==""))?0:(decimal.Parse(r["c11"].ToString()) * (decimal.Parse(nr["mucluongmoi"].ToString()) - decimal.Parse(r["c5"].ToString())) * decimal.Parse(r["luonghd"].ToString()));				
						nr["tdnangls"]=r["tdnangls"].ToString();
						nr["tongsocanbo"]=(ds1.Tables[0].Rows[0]["tongsocanbo"].ToString()=="")?0:decimal.Parse(ds1.Tables[0].Rows[0]["tongsocanbo"].ToString());
						nr["tongsocanbo_bl"]=(ds2.Tables[0].Rows[0]["tongsocanbo_bl"].ToString()=="")?0:decimal.Parse(ds2.Tables[0].Rows[0]["tongsocanbo_bl"].ToString());																					
						dsxml.Tables[0].Rows.Add(nr);						
					}
					else
					{
						dsxml.ReadXml("..\\xml\\BaoCaoN1.xml");
						dsxml.Clear();	
					}
				}
				dsxml.WriteXml("..\\xml\\BaoCaoN1.xml",XmlWriteMode.WriteSchema);			
				if (dsxml.Tables[0].Rows.Count>0)
				{
                    //if (prn) rpt.Printer(dsxml,"BaoCaoN1byt.rpt","",1);
                    //else
                    //{		
						frmReport f=new frmReport(mDB, dsxml,"","BaoCaoN1byt.rpt",true,0);
						f.ShowDialog(this);
					//}
				}
				else
					MessageBox.Show("Chưa có dữ liệu","Thông Báo");
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.ToString(),"Thông Báo");
			}
		}
		private void print2(bool prn)
		{	
			dsxml.ReadXml("..\\xml\\BaoCaoN3.xml");
			dsxml.Clear();
            string m_ngay = "",ymd="";

            m_ngay="28/"+cboThang.SelectedItem.ToString()+"/"+cboNam.SelectedItem.ToString();
            ymd =  cboNam.SelectedItem.ToString()+"-"+ cboThang.SelectedItem.ToString() + "-" +"28";

			string sql="SELECT '"+cboThang.SelectedItem.ToString()+"' as ct,";
			sql+=" '"+cboNam.SelectedItem.ToString()+"' as cn,bp.madv,bp.tenbp,";
			sql+=" trim(l.ho)||' '||trim(l.ten) as B,";
			sql+=" case when l.phai='Nam' then l.namsinh  else '' end  as c1,";
            sql += " case when l.phai='Nữ' then l.namsinh else '' end as c2,";
			sql+=" mn.diengiai as c3,hd.mangach as c4,hd.hs_lc as c5,";
			sql+=" to_char(hd.ngayhd,'dd/mm/yyyy') as c6,hd.mangach as c8,";
			sql+=" hd.hs_lc as mucluong,hd.hs_lc as mucluongmoi,hd.mangach,";
			sql+=" 00 as c10,hd.luonghd,hd.bac,";
            sql += "  '" + m_ngay + "' as c101,";
            sql += " (date_part('day',to_timestamp('" + ymd + "','yyyy-mm-dd')-to_timestamp(hd.ngayhd,'yyyy-mm-dd'))/30 ";
            sql += " - case when substring(trim(hd.loaingach),1)='A' then 3*12 else 2*12 end) as c11,";
			sql+=" 00 as c12,";
			sql+=" to_char("+mDB.user+".dateadd('m',case when substring(trim(hd.loaingach),1,1)='A' then 3*12 else 2*12 end,hd.ngayhd),'dd/mm/yyyy') as tdnangls";
            sql += " FROM " + mDB.user + ".llnv l inner join " + mDB.user + ".hopdong hd on l.manv=hd.manv ";
            sql += " inner join " + mDB.user + ".mahd mh on mh.mahd=hd.loaihd";
            sql += " inner join " + mDB.user + ".mangach mn on hd.mangach=mn.ma";
            sql += " inner join  " + mDB.user + ".bophan bp on l.madv=bp.madv";
			sql+=" WHERE hd.ngaynghi is null and hd.loaihd<>'HD' and l.madv=bp.madv ";
            sql += " and " + mDB.user + ".dateadd('m', case when substring(trim(hd.loaingach),1,1)='A' then 3*12 else 2*12 end, hd.ngayhd) is not null ";
            sql += " and " + mDB.user + ".dateadd('m', case when substring(trim(hd.loaingach),1,1)='A' then 3*12 else 2*12 end,hd.ngayhd) ";
            sql += " <=to_timestamp('" + m_ngay + "','yyyy-mm-dd' )";
			sql+=" and to_char(hd.ngayhd,'mm')="+cboThang.SelectedItem.ToString()+"";
			ds=mDB.get_data(sql);	
			
			string sql1=" SELECT sum(case when l.manv<>'' then  1 else 0 end) as tongsocanbo";
            sql1 += " FROM " + mDB.user + ".llnv l," + mDB.user + ".hopdong hd," + mDB.user + ".mahd mh," + mDB.user + ".mangach mn";
			sql1+=" WHERE hd.ngaynghi is null and l.manv=hd.manv and trim(hd.loaihd)<>'HD' and hd.loaihd=mh.mahd and hd.mangach=mn.ma";						
//			sql1+=" and hd.ngayhd <=(datevalue('28/'+'"+cboThang.SelectedItem.ToString()+"'+'/'+'"+cboNam.SelectedItem.ToString()+"'))";
//			sql1+=" and month(hd.ngayhd)="+cboThang.SelectedItem.ToString()+"";
			DataSet ds1=new DataSet();
			ds1=mDB.get_data(sql1);

			string sql2="SELECT Sum(case when l.manv Is Not Null then 1 else 0 end) AS tongsocanbo_bl";
            sql2 += " FROM " + mDB.user + ".llnv  l," + mDB.user + ".hopdong hd, " + mDB.user + ".mahd AS mh, " + mDB.user + ".mangach AS mn";
			sql2+=" WHERE hd.ngaynghi is null and (((l.manv)=hd.manv) AND ((hd.LOAIHD)<>'HD' And";
			sql2+=" (hd.LOAIHD)=mh.mahd) AND ((hd.MANGACH)=mn.ma)) and";
            sql2 += " " + mDB.user + ".dateadd('m', case when substring(trim(hd.loaingach),1,1)='A' then 3*12 else 2*12 end,";
            sql2 += " hd.ngayhd) is not null  and ";
            sql2 += "" + mDB.user + ".dateadd('m', case when substring(trim(hd.loaingach),1,1)='A' then 3*12 else 2*12 end,hd.ngayhd) ";
            sql2+="<=to_timestamp('" + m_ngay + "','yyyy-mm-dd' )";			
			sql2+=" and to_char(hd.ngayhd,'mm')='"+cboThang.SelectedItem.ToString()+"'";
			DataSet ds2=new DataSet();
			ds2=mDB.get_data(sql2);
            if(ds!=null)
            {
                foreach (DataRow r in ds.Tables[0].Rows)
                {
                    if (r["B"].ToString() != "")
                    {
                        DataRow nr = dsxml.Tables[0].NewRow();
                        nr["ct"] = (r["ct"].ToString() == "") ? "" : (r["ct"].ToString());
                        nr["cn"] = (r["cn"].ToString() == "") ? "" : (r["cn"].ToString());
                        nr["madv"] = (r["madv"].ToString() == "") ? "" : (r["madv"].ToString());
                        nr["tenbp"] = (r["tenbp"].ToString() == "") ? "" : (r["tenbp"].ToString());
                        nr["B"] = (r["B"].ToString() == "") ? "" : (r["B"].ToString());
                        nr["c1"] = (r["c1"].ToString());
                        nr["c2"] = (r["c2"].ToString());
                        nr["c3"] = (r["c3"].ToString());
                        nr["c4"] = (r["c4"].ToString());
                        nr["c5"] = (r["c5"].ToString() == "") ? 0 : decimal.Parse(r["c5"].ToString());
                        nr["c6"] = (r["c6"].ToString());
                        nr["c8"] = (r["c8"].ToString());
                        nr["bac"] = int.Parse(r["bac"].ToString());
                        nr["mucluong"] = (r["mucluong"].ToString() == "") ? 0 : decimal.Parse(r["mucluong"].ToString());
                        nr["mucluongmoi"] = mDB.next_hslc(r["mangach"].ToString(), decimal.Parse(r["mucluong"].ToString())).ToString();
                        nr["mangach"] = (r["mangach"].ToString());
                        nr["c101"] = (r["c101"].ToString());
                        nr["c10"] = (nr["mucluongmoi"].ToString() == "" && r["c5"].ToString() == "") ? 0 : (decimal.Parse(nr["mucluongmoi"].ToString()) - decimal.Parse(r["c5"].ToString()));
                        nr["c11"] = (r["c11"].ToString() == "") ? 0 : decimal.Parse(r["c11"].ToString());
                        nr["c12"] = (r["c11"].ToString() == "" && (nr["mucluongmoi"].ToString() == "") && (nr["c5"].ToString() == "")) ? 0 : (decimal.Parse(r["c11"].ToString()) * (decimal.Parse(nr["mucluongmoi"].ToString()) - decimal.Parse(r["c5"].ToString())) * 210000);
                        nr["tdnangls"] = r["tdnangls"].ToString();
                        nr["tongsocanbo"] = (ds1.Tables[0].Rows[0]["tongsocanbo"].ToString() == "") ? 0 : decimal.Parse(ds1.Tables[0].Rows[0]["tongsocanbo"].ToString());
                        nr["tongsocanbo_bl"] = (ds2.Tables[0].Rows[0]["tongsocanbo_bl"].ToString() == "") ? 0 : decimal.Parse(ds2.Tables[0].Rows[0]["tongsocanbo_bl"].ToString());
                        dsxml.Tables[0].Rows.Add(nr);
                    }
                    else
                    {
                        dsxml.ReadXml("..\\xml\\BaoCaoN3.xml");
                        dsxml.Clear();
                    }
                }
			}
			
			dsxml.WriteXml("..\\xml\\BaoCaoN3.xml",XmlWriteMode.WriteSchema);
			
			if (dsxml.Tables[0].Rows.Count>0)
			{
				if (prn)
				{
					ExportToExcel exp =new ExportToExcel();
					exp.exp_excel(dsxml,"danhsachNangluong");
//					rpt.Printer(dsxml,"BaoCaoN3byt.rpt","",1);
				}
				else
				{		
					frmReport f=new frmReport(mDB, dsxml,"","BaoCaoN3byt.rpt",true,0);
					f.ShowDialog(this);
				}
			}
			else
				MessageBox.Show("Chưa có dữ liệu !","Thông Báo");
		}
		private void frmDanhSachVaKetQuaNangLuong_Load(object sender, System.EventArgs e)
		{
			DateTime dt=DateTime.Now,dt1;
			dt1=dt.AddYears(1);
			cboThang.SelectedItem=dt.Month.ToString().PadLeft(2,'0');
			cboNam.SelectedItem=dt.Year.ToString();			
		}

		private void butXem_Click(object sender, System.EventArgs e)
		{
			print2(false);
			butThoat.Focus();
		}

		private void butIn_Click(object sender, System.EventArgs e)
		{
			print2(true);
			butThoat.Focus();
		}

		private void cboThang_Validated(object sender, System.EventArgs e)
		{
			if(int.Parse(cboThang.Text)<0 || int.Parse(cboThang.Text)>12)
			{
				MessageBox.Show("Tháng không hợp lệ","Thông báo");
				cboThang.Focus();
			}
		}

		private void cboThang_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode.ToString()=="Enter")
			{
				SendKeys.Send("{Tab}");
			}
		}

		private void cboNam_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode.ToString()=="Enter")
			{
				SendKeys.Send("{Tab}");
			}
		}

		private void checkM1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode.ToString()=="Enter")
			{
				SendKeys.Send("{Tab}");
			}
		}	
	}
}
